package com.hc.oa.project.service;

import java.sql.Date;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.hibernate.annotations.common.util.StringHelper;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.data.provider.Page;
import com.hc.oa.model.PmProject;
import com.hc.oa.project.dao.PmProjectDao;


@Component("com.hc.projectService")
public class ProjectService {
	//
	
	@Resource
	private PmProjectDao PmProjectDao;
	
//	@DataProvider
//	public void getAssertion(Page<PmProject> page) throws Exception{
//		PmProjectDao.queryAllByPage(PmProject.class, page);
//	  }
	
	 @DataProvider
	    public void getAllForPage(Page<PmProject> page) throws Exception{
		 PmProjectDao.queryAllByPage(PmProject.class, page);
	    }
	@DataResolver
	@Transactional
	public void saveAssertion(Collection<PmProject> datas) throws Exception{
		PmProjectDao.persistEntities(datas);
	}

	 @DataProvider
	    public void setAllForPage(Page<PmProject> page) throws Exception{
		 PmProjectDao.queryAllByPage(PmProject.class, page);
	    }
	
	 @DataResolver
	    @Transactional
	    public void saveMessage(Collection<PmProject> datas) throws Exception{
	        
	    	for(PmProject pmProject:datas){
	    		
	    		pmProject.setCreateDate(new Date(0));;
	    	}
	    
	    	PmProjectDao.persistEntities(datas);
	    }
	 
	

	 @DataResolver
	 @Transactional
	    public void saveAll(Collection<PmProject> PmProject){
		 PmProjectDao.persistEntities(PmProject);
	    }
	  
	 
	 @DataProvider
	 @Transactional
     public void queryForCondition(Page<PmProject> page,Map<String, Object> params) {
			if (null != params) {
	        int project_id = Integer.valueOf((String)params.get("project_id"));

             String project_name = (String) params.get("project_name");
               String parent_id = (String)params.get("parent_id");
             String project_manager = (String)params.get("project_manager");
             String project_type_id = (String)params.get("project_type_id");
             
             Date plan_start_date = (Date)params.get("plan_start_date");
             Date plan_delivery_date = (Date) params.get("plan_delivery_date");
             Date actual_start_date = (Date)params.get("actual_start_date");
             Date actual_delivery_date = (Date)params.get("actual_delivery_date");
             
             float percent_completed = Float.valueOf((String)params.get("percent_completed"));
             int ontrack_status_id = Integer.valueOf((String)params.get("ontrack_status_id"));
             int project_priority_id = Integer.valueOf((String)params.get("project_priority_id"));
             int project_status_id = Integer.valueOf((String)params.get("project_status_id"));
             
             String creator = (String)params.get("creator");
             Date create_date = (Date)params.get("create_date");
             String updator = (String)params.get("updator");
             Date update_date = (Date)params.get("update_date");
             String description = (String)params.get("description");
             
             String whereCase = "";
             
             if (project_id!=0) {
                 whereCase += " AND project_id like '%" + project_id + "%' ";
             }
              
             if (StringHelper.isNotEmpty(project_name)) {
                 whereCase += " AND project_name '%" + project_name + "%' ";
             }
              
             if (StringHelper.isNotEmpty(parent_id)) {
                 whereCase += " AND parent_id '%" + parent_id + "%' ";
             }
              
             if (StringHelper.isNotEmpty(project_manager)) {
                 whereCase += " project_manager '%" + project_manager + "%' ";
             }
              
             if (StringHelper.isNotEmpty(project_type_id)) {
                 whereCase += " AND project_type_id like '%" + project_type_id + "%' ";
             }
              
             if (StringHelper.isNotEmpty(plan_start_date.toString())) {
                 whereCase += " AND plan_start_date like '%" + plan_start_date + "%' ";
             }
              
             if (StringHelper.isNotEmpty(plan_delivery_date.toString())) {
                 whereCase += " AND plan_delivery_date like '%" + plan_delivery_date + "%' ";
             }
             
             if (StringHelper.isNotEmpty(actual_start_date.toString())) {
                 whereCase += " AND actual_start_date like '%" + actual_start_date + "%' ";
             }
             
             if (StringHelper.isNotEmpty(actual_delivery_date.toString())) {
                 whereCase += " AND actual_delivery_date like '%" + actual_delivery_date + "%' ";
             }
             
             if (percent_completed!=0) {
                 whereCase += " AND percent_completed like '%" + percent_completed + "%' ";
             }
             
             if (ontrack_status_id!=0) {
                 whereCase += " AND ontrack_status_id like '%" + ontrack_status_id + "%' ";
             }
             
             if (project_priority_id!=0) {
                 whereCase += " AND position like '%" + project_priority_id + "%' ";
             }
             
             if (project_status_id !=0) {
                 whereCase += " AND project_status_id like '%" + project_status_id + "%' ";
             }
         
             
             if (StringHelper.isNotEmpty(creator.toString())) {
                 whereCase += " AND creator like '%" + creator + "%' ";
             }
              
             if (StringHelper.isNotEmpty(create_date.toString())) {
                 whereCase += " AND create_date like '%" + create_date + "%' ";
             }
             
             if (StringHelper.isNotEmpty(updator.toString())) {
                 whereCase += " AND updator like '%" + updator + "%' ";
             }
             
             if (StringHelper.isNotEmpty(update_date.toString())) {
                 whereCase += " AND update_date like '%" + update_date + "%' ";
             }
             
             if (StringHelper.isNotEmpty(description.toString())) {
                 whereCase += " AND description like '%" + description + "%' ";
             }
             
             
             PmProjectDao.find(page, " from pm_project where 1=1 " + whereCase);
         } else
        	 PmProjectDao.getAll(page);
     }
         
	 @DataProvider
	 @Transactional
	    public void getParentId(Page<PmProject> page){
	       
	            String hql = "select project_id from pm_project where 1=1";
	            Map<String, Integer> param = new HashMap<String, Integer>();
	            PmProjectDao.find(hql, param);
	             
	    }
	 
}
